#pragma once
#include <iostream>
#include <string>
#include "Init.hpp"

enum
{
    DivZero = 1,
    ModZero,
    Unknown
};

Init i;

class Task
{
public:
    Task()
    {
    }
    Task(int sockfd, const std::string& ip, const uint16_t& port)
        : sockfd_(sockfd), clientport(port), clientip(ip)
    {
    }

    void run()
    {
        char buffer[1024];
        ssize_t n = read(sockfd_, buffer, sizeof(buffer));
        if (n > 0)
        {
            buffer[n] = 0;

            std::cout << "client say " << clientip << "#" << buffer << std::endl;

            std::string message;
            message = "server say#";
            message += i.transaction(buffer);

            ssize_t s = write(sockfd_, message.c_str(), message.size());
            if (s < 0)
            {
                std::cout << "Warning,write failed" << std::endl;
            }
        }
        else if (n == 0)
        {
            std::cout << "client " << clientip << " : " << clientport << "is closed ,me too!!!" << std::endl;
        }
        else
        {
            std::cout << "read error," << std::endl;
        }
        close(sockfd_);
    }

    void operator()()
    {
        run();
    }

private:
    int sockfd_;
    uint16_t clientport;
    std::string clientip;
};